package crm.view.Admin;

//   功能已实现

import crm.dao.impl.CustomerSqlSeverImpl;
import crm.entity.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 客户管理：
 * 管理员对客户注册、删除修改基本信息
 */
public class UpdateCustomer implements ActionListener {
    JFrame frame = null;
    JLabel id = null;
    JLabel name = null;
    JLabel pwd = null;
    JLabel phone = null;
    JLabel address = null;
    JLabel credit = null;
    JLabel products = null;
    JLabel contactId = null;

    JTextField IdField = null;
    JTextField NameField = null;
    JTextField PwdField = null;
    JTextField PhoneField = null;
    JTextField AddressField = null;
    JTextField CreditField = null;
    JTextField ProductionField = null;
    JTextField ContactIdField = null;
    JButton btn2 = null;
    JButton btn3 = null;

    JLabel label = null;

    public static void main(String[] args) {
        new UpdateCustomer();
    }

    public UpdateCustomer() {
        init();
        frame.setBounds(680, 200, 600, 700);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("客户基本信息");
        id = new JLabel("修改编号:");
        name = new JLabel("输入用户姓名:");
        pwd = new JLabel("修改密码:");
        phone = new JLabel("修改联系方式:");
        address = new JLabel("修改联系地址:");
        credit = new JLabel("修改信用度:");
        products = new JLabel("修改所购产品:");
        contactId = new JLabel("修改联系人编号:");
        label = new JLabel("修改客户信息");
        IdField = new JTextField("");
        NameField = new JTextField("");
        PwdField = new JTextField("");
        PhoneField = new JTextField("");
        AddressField = new JTextField("");
        CreditField = new JTextField("");
        ProductionField = new JTextField("");

        ContactIdField = new JTextField("");

        btn2 = new JButton("清空");
        btn3 = new JButton("修改客户");
        frame.add(btn2);
        frame.add(btn3);
        frame.add(label);
        Font font1 = new Font("方正黑体简体",1,30);
        label.setFont(font1);

        Font font = new Font("方正楷体", 1, 20);
        id.setFont(font);
        IdField.setFont(font);
        name.setFont(font);
        NameField.setFont(font);
        pwd.setFont(font);
        PwdField.setFont(font);
        phone.setFont(font);
        PhoneField.setFont(font);
        btn2.setFont(font);
        btn3.setFont(font);

        address.setFont(font);
        AddressField.setFont(font);
        credit.setFont(font);
        CreditField.setFont(font);
        products.setFont(font);
        ProductionField.setFont(font);
        contactId.setFont(font);
        ContactIdField.setFont(font);


        frame.add(id);
        frame.add(IdField);
        frame.add(name);
        frame.add(NameField);
        frame.add(pwd);
        frame.add(PwdField);
        frame.add(phone);
        frame.add(PhoneField);
        frame.add(address);
        frame.add(AddressField);
        frame.add(credit);
        frame.add(CreditField);
        frame.add(products);
        frame.add(ProductionField);

        frame.add(contactId);
        frame.add(ContactIdField);

        label.setBounds(200,20,200,30);
        name.setBounds(40, 90, 140, 60);
        NameField.setBounds(200, 100, 300, 40);

        id.setBounds(40, 140, 100, 60);
        IdField.setBounds(200, 150, 300, 40);

        pwd.setBounds(40, 190, 100, 60);
        PwdField.setBounds(200, 200, 300, 40);

        phone.setBounds(40, 240, 140, 60);
        PhoneField.setBounds(200, 250, 300, 40);

        address.setBounds(40, 290, 140, 60);
        AddressField.setBounds(200, 300, 300, 40);

        credit.setBounds(40, 340, 140, 60);
        CreditField.setBounds(200, 350, 300, 40);

        products.setBounds(40, 390, 140, 60);
        ProductionField.setBounds(200, 400, 300, 40);

        contactId.setBounds(40, 440, 160, 60);
        ContactIdField.setBounds(200, 450, 300, 40);

        btn2.setBounds(320, 500, 100, 35);
        btn3.setBounds(120, 500, 140, 35);

        btn2.addActionListener(this);
        btn3.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        CustomerSqlSeverImpl dao = new CustomerSqlSeverImpl();
        Customer customer = new Customer();

        if ("修改客户".equals(e.getActionCommand())) {
            customer.setCustomer_id(Integer.valueOf(IdField.getText()));
            customer.setCustomer_name(NameField.getText());
            customer.setCustomer_pwd(PwdField.getText());
            customer.setCustomer_phone(PhoneField.getText());
            customer.setProducts(ProductionField.getText());
            customer.setCustomer_address(PwdField.getText());
            customer.setCredit(CreditField.getText());
            customer.setContact_id(ContactIdField.getText());
            int i = dao.Update(customer);
            System.out.println(i);
            if (i > 0) {
                JOptionPane.showMessageDialog(null, "修改成功！");
            }

        } else if ("清空".equals(e.getActionCommand())) {
            IdField.setText("");
            NameField.setText("");
            PwdField.setText("");
            CreditField.setText("");
            ContactIdField.setText("");
            ProductionField.setText("");
            PhoneField.setText("");
            AddressField.setText("");
            ContactIdField.setText("");
        }
    }
}
